package com.molichuxing.gateway.api.services.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.api.services.StockApiService;
import com.molichuxing.gateway.bff.stock.entity.request.modify.StockCarModifyVo;
import com.molichuxing.gateway.bff.stock.entity.response.EntrepotStorageVo;
import com.molichuxing.gateway.bff.stock.entity.response.FenceBaseVo;
import com.molichuxing.gateway.bff.stock.entity.response.StockCarDetailVo;
import com.molichuxing.gateway.bff.stock.entity.response.StockCarVo;
import com.molichuxing.gateway.bff.stock.entity.response.StockRecordBaseVo;
import com.molichuxing.gateway.bff.stock.services.StockCarBffService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author liyg
 * 库存车辆web接口实现类
 * 2019-09-09
 *
 */
@Api(tags = "库存相关接口")
@RestController
@RequestMapping(value = "/stock")
public class StockApiServiceImpl extends RootApiService implements StockApiService{
	
	private static final Logger logger = LoggerFactory.getLogger(StockApiServiceImpl.class);

	@Resource
	private StockCarBffService stockCarBffService;
	


	/**
	 * 获取车辆调拨列表
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取车辆调拨列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "vin", value = "车架号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "licencePlate", value = "车牌号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "vehicleId", value = "车型id", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "vehicleType", value = "车辆类型（0：新车，1：二手车）", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "storeId", value = "门店id", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "sortName", value = "排序字段", required = false, dataType = "String"),
			@ApiImplicitParam(name = "sortWay", value = "排序方式 ", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/allot/list")
	public ResponseResult<Paged<StockCarVo>> getStockCarList(@RequestParam Map<String, Object> map) {
		
		ResponseResult<Paged<StockCarVo>> result = new ResponseResult<>();
		try {
			Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
			if(null==pageNum || pageNum < 1 ) {
				map.put("pageNum",1);
			}
			Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
			if(null==pageSize || pageSize <= 0) {
				map.put("pageSize",10);
			}
			return result.success(stockCarBffService.getStockCarList(map));
		}catch (BizException e) {
			logger.error("[getStockCarList]获取库存列表信息异常", e.getMessage());
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (Exception e) {
			logger.error("[getStockCarList]获取库存列表信息异常", e);
			result.error("获取库存列表信息异常");
		}
		return result;
	}
	/**
	 * 获取到期还车列表（待还车 和 回库中）
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取到期还车列表（待还车 和 回库中）")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "tabType", value = "tab值", required = false, dataType = "Integer"),
		@ApiImplicitParam(name = "vin", value = "车架号", required = false, dataType = "String"),
		@ApiImplicitParam(name = "licencePlate", value = "车牌号", required = false, dataType = "String"),
		@ApiImplicitParam(name = "sortName", value = "排序字段", required = false, dataType = "String"),
		@ApiImplicitParam(name = "sortWay", value = "排序方式 ", required = false, dataType = "String"),
		@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
		@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/return/list")
	public ResponseResult<Paged<StockCarDetailVo>> getReturnList(@RequestParam Map<String, Object> map) {
		
		ResponseResult<Paged<StockCarDetailVo>> result = new ResponseResult<>();
		try {
			Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
			if(null==pageNum || pageNum < 1 ) {
				map.put("pageNum",1);
			}
			Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
			if(null==pageSize || pageSize <= 0) {
				map.put("pageSize",10);
			}
			return result.success(stockCarBffService.getReturnList(map));
		}catch (BizException e) {
			logger.error("[getReturnList]获取到期还车列表（待还车 和 回库中）异常", e.getMessage());
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (Exception e) {
			logger.error("[getReturnList]获取到期还车列表（待还车 和 回库中）异常", e);
			result.error("获取到期还车列表（待还车 和 回库中）异常");
		}
		return result;
	}
	
	/**
	 * 获取到期还车列表（已回库）
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取到期还车列表（已回库）")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "tabType", value = "tab值", required = false, dataType = "Integer"),
		@ApiImplicitParam(name = "vin", value = "车架号", required = false, dataType = "String"),
		@ApiImplicitParam(name = "licencePlate", value = "车牌号", required = false, dataType = "String"),
		@ApiImplicitParam(name = "sortName", value = "排序字段", required = false, dataType = "String"),
		@ApiImplicitParam(name = "sortWay", value = "排序方式 ", required = false, dataType = "String"),
		@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
		@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/return/record/list")
	public ResponseResult<Paged<StockCarDetailVo>> getReturnRecordList(@RequestParam Map<String, Object> map) {
		
		ResponseResult<Paged<StockCarDetailVo>> result = new ResponseResult<>();
		try {
			Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
			if(null==pageNum || pageNum < 1 ) {
				map.put("pageNum",1);
			}
			Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
			if(null==pageSize || pageSize <= 0) {
				map.put("pageSize",10);
			}
			return result.success(stockCarBffService.getReturnRecordList(map));
		}catch (BizException e) {
			logger.error("[getReturnRecordList]获取到期还车列表（已回库）异常", e.getMessage());
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (Exception e) {
			logger.error("[getReturnRecordList]获取到期还车列表（已回库）异常", e);
			result.error("获取到期还车列表（已回库）异常");
		}
		return result;
	}
	
	
	/**
	 *获取库存车辆出库详情
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取库存车辆出库详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "carId", value = "车辆id", required = true, dataType = "Integer")

	})
	@GetMapping(value = "/allot/detail")
	public ResponseResult<StockCarVo> getStockCar(@RequestParam Integer carId) {
		
		ResponseResult<StockCarVo> result = new ResponseResult<>();
		try {
			return result.success(stockCarBffService.getStockCar(carId));
		}catch (BizException e) {
			logger.error("[getStockCar]获取库存车辆出库详情异常", e.getMessage());
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (Exception e) {
			logger.error("[getStockCar]获取库存车辆出库详情异常", e);
			result.error("获取库存车辆出库详情异常");
		}
		return result;
	}
	
	/**
	 *车辆调拨信息
	 * @param modifyVo
	 * @return
	 * 
	 */
	@Override
	@ApiOperation(value = "车辆调拨信息")
	@PostMapping(value = "/allot/modify")
	public ResponseResult<Boolean> modifyAllotStock(@RequestBody StockCarModifyVo modifyVo){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			return result.success(stockCarBffService.modifyAllot(modifyVo));
		}catch (BizException e) {
			logger.error("[modifyAllotStock]车辆调拨信息异常", e.getMessage());
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (Exception e) {
			logger.error("[modifyAllotStock]车辆调拨信息异常", e);
			result.error("车辆调拨信息异常");
		}
		return result;
	}
	
	/**
	 *还车
	 * @param modifyInStock
	 * @return
	 * 
	 */
	@Override
	@ApiOperation(value = "还车")
	@PostMapping(value = "/return/modify")
	public ResponseResult<Boolean> modifyReturn(@RequestBody StockCarModifyVo modifyVo){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			return result.success(stockCarBffService.modifyReturn(modifyVo));
		}catch (BizException e) {
			logger.error("[modifyReturn]还车异常", e.getMessage());
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (Exception e) {
			logger.error("[modifyReturn]还车异常", e);
			result.error("还车异常");
		}
		return result;
	}
	
	/**
	 *获取中转库下拉列表
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取中转库下拉列表")
	@GetMapping(value = "/entrepot/base/list")
	public ResponseResult<List<EntrepotStorageVo>> getList(){
		
		ResponseResult<List<EntrepotStorageVo>> result = new ResponseResult<>();
		try {
			return result.success(stockCarBffService.getEntrepotStorageList());
		}catch (BizException e) {
			logger.error("[getEntrepotStorageList]获取中转库下拉列表信息异常", e.getMessage());
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (Exception e) {
			logger.error("[getEntrepotStorageList]获取中转库下拉列表信息异常", e);
			result.error("获取中转库下拉列表信息异常");
		}
		return result;
	}
	
	/**
	 *根据车辆id获取库存历史流转记录
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "根据车辆id获取库存（车辆）历史流转记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "carId", value = "车辆id", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/moving/list")
	public ResponseResult<List<StockRecordBaseVo>> getListByCarId(@RequestParam Integer carId) {
		
		ResponseResult<List<StockRecordBaseVo>> result = new ResponseResult<>();
		try {
			return result.success(stockCarBffService.getListByCarId(carId));
		}catch (BizException e) {
			logger.error("[getListByCarId]根据车辆id获取库存（车辆）历史流转记录异常", e.getMessage());
			result.error(e.getExceptionCode(),e.getMessage());
		} catch (Exception e) {
			logger.error("[getListByCarId]根据车辆id获取库存（车辆）历史流转记录异常", e);
			result.error("根据车辆id获取库存（车辆）历史流转记录异常");
		}
		return result;
	}
	
	/**
	 * 根据门店或者中转库  id 查询 围栏数据
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "根据门店或者中转库查 围栏数据信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "门店id或中转库id", required = false, dataType = "Integer"),
		@ApiImplicitParam(name = "stockType", value = "类型（1：中转库，2：门店）", required = false, dataType = "Integer")
	})
	@GetMapping(value = "/entrepot/store/fence")
	public ResponseResult<List<FenceBaseVo>> getEntrepotOrStoreFence(@RequestParam Map<String, Object> map) {
		
		ResponseResult<List<FenceBaseVo>> result = new ResponseResult<>();
		try {
			return result.success(stockCarBffService.getEntrepotOrStoreFence(map));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getEntrepotOrStoreFence]根据门店或者中转库查 围栏数据信息异常", e);
			result.error("根据门店或者中转库查 围栏数据信息异常");
		}
		return result;
	}
}
